package com.ton.system.service;

import com.ton.system.entity.AdminPermission;
import com.ton.system.entity.AdminRole;
import com.ton.system.entity.AdminRolePermission;
import com.ton.utils.SqlArgUtil;
import com.kypj.frame.common.EmptyUtil;
import com.kypj.frame.common.NumberUtil;
import com.kypj.frame.db.MyTemplate;
import com.kypj.frame.db.Pager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class RoleService {
    @Resource
    private MyTemplate myTemplate;

    public List<AdminPermission> listAdminPermission() {
        return myTemplate.myQueryForListAppendWhere(AdminPermission.class, null);
    }

    public AdminRole findAdminRoleByName(String roleName) {
        return myTemplate.myGetObjectAppendWhere(AdminRole.class, "role_name=?", roleName);
    }

    public AdminRole findAdminRoleById(Integer id) {
        return myTemplate.myGetObjectByPK(AdminRole.class, id);
    }

    public List<AdminPermission> listAdminPermission(Integer roleId) {
        return myTemplate.myQueryForListAppendWhere(AdminPermission.class,
                "id in (select permission from admin_role_permission where role_id=?)", roleId);
    }

    public List<AdminRole> listAdminRole(Pager pager, String name) {
        String search = SqlArgUtil.like(name);
        int count = myTemplate.myCountAppendWhereCondition(AdminRole.class, "#role_name=?", search);
        pager.setCount(count);
        return myTemplate.myQueryForListAppendWhereCondition(AdminRole.class, "#role_name=? limit ?,?", search, pager.limitStart(), pager.limitEnd());
    }

    public List<AdminRole> listAdminRole() {
        return myTemplate.myQueryForListAppendWhere(AdminRole.class, null);
    }

    public List<AdminRole> listAdminRoleByUserId(Integer userId) {
        return myTemplate.myQueryForListAppendWhere(AdminRole.class,
                "id in (select role_id from admin_user_role where admin_user_id=?)", userId);
    }

    public void addAdminRole(AdminRole role, String permissionIds) {
        myTemplate.mySaveObject(role);
        if (EmptyUtil.isNotEmpty(permissionIds)) {
            String[] ids = permissionIds.split(",");
            for (String permissionId : ids) {
                if (NumberUtil.isInteger(permissionId)) {
                    AdminRolePermission rp = new AdminRolePermission();
                    rp.setPermission(Integer.parseInt(permissionId));
                    rp.setRoleId(role.getId());
                    myTemplate.mySaveObject(rp);
                }
            }
        }
    }

    public void updateAdminRole(AdminRole role, String permissionIds) {
        myTemplate.myUpdateObject(role);
        myTemplate.myUpdate("delete from admin_role_permission where role_id=?", role.getId());
        String[] ids = permissionIds.split(",");
        for (String permissionId : ids) {
            if (NumberUtil.isInteger(permissionId)) {
                AdminRolePermission rp = new AdminRolePermission();
                rp.setPermission(Integer.parseInt(permissionId));
                rp.setRoleId(role.getId());
                myTemplate.mySaveObject(rp);
            }
        }
    }

    public void deleteAdminRole(Integer roleId) {
        myTemplate.myDeleteObjectByPK(AdminRole.class, roleId);
        myTemplate.myUpdate("delete from admin_role_permission where role_id=?", roleId);
        myTemplate.myUpdate("delete from admin_user_role where role_id=?", roleId);

    }

    public AdminPermission getAdminPermission(String uri) {
        AdminPermission adminPermission = myTemplate.myGetObjectAppendWhere(AdminPermission.class, "uri=?", uri);
        return adminPermission;
    }

}
